const Sequelize = require('sequelize');
const moment = require('moment');
const hooks = require('./sequelizeHooks');

module.exports = function (sequelize, DataTypes) {
  const fields = {
    id: {
      autoIncrement: true,
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      comment: 'id'
    },
    uid: {
      type: DataTypes.BLOB,
      allowNull: true,
      comment: 'uid'
    },
    tenant_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: 0,
      primaryKey: true,
      comment: '租户id'
    },
    material_no: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '物料编码'
    },
    material_name: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '物料名称'
    },
    material_model: {
      type: DataTypes.STRING(1000),
      allowNull: true,
      comment: '物料规格'
    },
    material_name_model: {
      type: DataTypes.STRING(1200),
      allowNull: true,
      comment: '物料名称与型号'
    },
    material: {
      type: DataTypes.STRING(1000),
      allowNull: true,
      comment: '材质或色号'
    },
    material_unit: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '计量单位'
    },
    brand: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '品牌'
    },
    origin: {
      type: DataTypes.STRING(200),
      allowNull: true,
      comment: '产地'
    },
    barcode: {
      type: DataTypes.STRING(100),
      allowNull: true,
      comment: '条码编号'
    },
    images: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '图片'
    },
    default_image: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '物料首图'
    },
    catalog_id1: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '分类1'
    },
    catalog_id2: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '分类2'
    },
    catalog_id3: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '分类3'
    },
    catalog_id4: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '分类4'
    },
    remark: {
      type: DataTypes.STRING(500),
      allowNull: true,
      comment: '备注'
    },
    sale_price: {
      type: DataTypes.DECIMAL(10, 4),
      allowNull: true,
      defaultValue: 0.0000,
      comment: '销售单价'
    },
    describe: {
      type: DataTypes.TEXT,
      allowNull: true,
      comment: '销售描述，富文本'
    },
    sell: {
      type: DataTypes.SMALLINT,
      allowNull: true,
      defaultValue: 0,
      comment: '是否上架销售，0-否 1-是'
    },
    pv: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: 0,
      comment: '浏览量'
    },
    recommend: {
      type: DataTypes.SMALLINT,
      allowNull: true,
      defaultValue: 0,
      comment: '是否推荐，0-否 1-是'
    },
    history: {
      type: DataTypes.JSON,
      allowNull: true,
      comment: '历史'
    },
    create_time: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '创建时间'
    },
    create_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '创建人ID'
    },
    create_user_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '创建人姓名'
    },
    modify_time: {
      type: DataTypes.BIGINT,
      allowNull: true,
      comment: '修改时间'
    },
    modify_user_id: {
      type: DataTypes.INTEGER,
      allowNull: true,
      comment: '修改人id'
    },
    modify_user_name: {
      type: DataTypes.STRING(50),
      allowNull: true,
      comment: '修改人姓名'
    },
    create_date: {
      type: DataTypes.VIRTUAL,
      get () {
        const val = this.getDataValue('create_time');
        return val && moment(val, 'X').format('YYYY/MM/DD');
      }
    },
    modify_date: {
      type: DataTypes.VIRTUAL,
      get () {
        const val = this.getDataValue('modify_time');
        return val && moment(val, 'X').format('YYYY/MM/DD');
      }
    }
  };

  return sequelize.define('t_materials', fields, {
    sequelize,
    tableName: 't_materials',
    hasTrigger: true,
    timestamps: false,
    freezeTableName: true,
    indexes: [
      {
        name: 'PRIMARY',
        unique: true,
        using: 'BTREE',
        fields: [
          { name: 'id' },
          { name: 'tenant_id' }
        ]
      },
      {
        name: 'idx_uid',
        using: 'BTREE',
        fields: [
          { name: 'uid' }
        ]
      },
      {
        name: 'idx_tenant_id',
        using: 'BTREE',
        fields: [
          { name: 'tenant_id' }
        ]
      },
      {
        name: 'idx_cata1',
        using: 'BTREE',
        fields: [
          { name: 'catalog_id1' }
        ]
      },
      {
        name: 'idx_cata2',
        using: 'BTREE',
        fields: [
          { name: 'catalog_id2' }
        ]
      },
      {
        name: 'idx_cata3',
        using: 'BTREE',
        fields: [
          { name: 'catalog_id3' }
        ]
      },
      {
        name: 'idx_cata4',
        using: 'BTREE',
        fields: [
          { name: 'catalog_id4' }
        ]
      },
      {
        name: 'idx_cata',
        using: 'BTREE',
        fields: [
          { name: 'catalog_id1' },
          { name: 'catalog_id2' },
          { name: 'catalog_id3' },
          { name: 'catalog_id4' }
        ]
      }
    ],
    hooks: {
      beforeCreate (record, options) {
        hooks.singleBeforeCreate(record, options);
      },
      beforeBulkCreate (records, options) {
        records.forEach(row => {
          hooks.singleBeforeCreate(row, options);
        });
      },
      beforeUpdate (record, options) {
        hooks.singleBeforeUpdate(record, options, fields);
      }
    }
  });
};
